﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Batch.Amazon.ECS;
using Simple;
using TagCore;

namespace AmazonQuery
{
    public class AmazonSearch : IMusicQuery
    {
        #region Data
        string accessKeyId;
        string secretKey;
        string endPoint;
        AWSECommerceServicePortTypeClient client;
        #endregion

        #region C'tor
        public AmazonSearch(string accessKeyId, string secretKey, string endPoint)
        {
            this.accessKeyId = accessKeyId;
            this.secretKey = secretKey;
            this.endPoint = endPoint;

            // create a WCF Amazon ECS client
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
            binding.MaxReceivedMessageSize = int.MaxValue;
            client = new AWSECommerceServicePortTypeClient(
                binding,
                new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));

            // add authentication to the ECS client
            client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));
        }
        #endregion

        #region IMusicQuery
        public string Provider()
        {
            return "Amazon ECS";
        }

        public IMusicData[] Search(IMusicData data, QueryFields fields)
        {
            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "Music";

            if ((fields & QueryFields.Artist) == QueryFields.Artist)
                request.Artist = data.Artists[0];
            if ((fields & QueryFields.Title) == QueryFields.Title)
                request.Title = data.Title;
            if ((fields & QueryFields.Album) == QueryFields.Album)
                request.Title = data.Album;
            if ((fields & QueryFields.Year) == QueryFields.Year)
                request.ReleaseDate = data.Year.ToString();

            request.ResponseGroup = new string[] { "Images", "ItemAttributes", "BrowseNodes", "Tracks" };

            ItemSearch itemSearch = new ItemSearch();
            itemSearch.Request = new ItemSearchRequest[] { request };
            itemSearch.AWSAccessKeyId = accessKeyId;

            // issue the ItemSearch request
            ItemSearchResponse response = client.ItemSearch(itemSearch);

            // Get first response
            List<IMusicData> musicData = new List<IMusicData>();
            if (response.Items.Length > 0)
            {
                if (response.Items[0].Item != null)
                {
                    foreach (Item item in response.Items[0].Item)
                        musicData.Add(new AmazonResponse(item, data));
                }
            }
            return musicData.ToArray();
        }
        #endregion

        #region Public Overrides
        public override string ToString()
        {
            return Provider();
        }
        #endregion
    }
}
